package com.spring.hot;

import java.util.HashMap;

/**
 * @author Cizai
 * @since 2022/11/30 21:41
 **/
public class 无重复的最长子串 {

    public int lengthOfLongestSubstring(String s) {

        if (s.length() == 1) return 1;

        HashMap<Character, Integer> map = new HashMap<>();
        int max = 0;
        for (int start = 0,end = 0; end < s.length(); end++) {

            if (map.containsKey(s.charAt(end))){
                start = Math.max(start,map.get(s.charAt(end))+1);
            }

            map.put(s.charAt(end),end);
            max = Math.max(max, end-start+1);
        }
        return max;
    }
}
